package com.teamone.etl.accountant.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.accountant.dao.RechargeSheetDao;
import com.teamone.etl.accountant.dao.TicketAddressDao;
import com.teamone.etl.accountant.dto.InvoicingDto;
import com.teamone.etl.accountant.entity.InvoiceSpecification;
import com.teamone.etl.accountant.dao.InvoiceSpecificationDao;
import com.teamone.etl.accountant.entity.RechargeSheet;
import com.teamone.etl.accountant.entity.TicketAddress;
import com.teamone.etl.accountant.service.InvoiceSpecificationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 发票明细表(InvoiceSpecification)表服务实现类
 *
 * @author makejava
 * @since 2021-01-22 10:48:37
 */
@Service("invoiceSpecificationService")
@Transactional(rollbackFor = Exception.class)
public class InvoiceSpecificationServiceImpl implements InvoiceSpecificationService {
    @Resource
    private RechargeSheetDao rechargeSheetDao;

    @Resource
    private InvoiceSpecificationDao invoiceSpecificationDao;

    @Resource
    private TicketAddressDao ticketAddressDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InvoiceSpecification queryById(Integer id) {
        return this.invoiceSpecificationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<InvoiceSpecification> queryAllByLimit(int offset, int limit) {
        return this.invoiceSpecificationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param invoiceSpecification 实例对象
     * @return 实例对象
     */
    @Override
    public InvoiceSpecification insert(InvoiceSpecification invoiceSpecification) {
        this.invoiceSpecificationDao.insert(invoiceSpecification);
        return invoiceSpecification;
    }

    /**
     * 修改数据
     *
     * @param invoiceSpecification 实例对象
     * @return 实例对象
     */
    @Override
    public InvoiceSpecification update(InvoiceSpecification invoiceSpecification) {
        this.invoiceSpecificationDao.update(invoiceSpecification);
        return this.queryById(invoiceSpecification.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.invoiceSpecificationDao.deleteById(id) > 0;
    }

    @Override
    public int invoicing(InvoicingDto invoicingDto) {
        System.out.println("前台传过来的字段:"+invoicingDto);
        //根据充值编号查询对应记录
        RechargeSheet rechargeSheet = rechargeSheetDao.findRechargeId(invoicingDto.getRechargeId());
        System.out.println("充值记录:"+rechargeSheet);
        //添加发票申请记录表
        InvoiceSpecification invoiceSpecification = new InvoiceSpecification();
        invoiceSpecification.setInvoiceTitle(invoicingDto.getInvoiceTitle());
        invoiceSpecification.setInvoiceType(invoicingDto.getInvoiceType());
        invoiceSpecification.setInvoiceMoney(rechargeSheet.getTradingMoney());
        invoiceSpecification.setInvoiceStatus(3);
        invoiceSpecification.setInvoicereqTime(new Date());
        invoiceSpecification.setCreateTime(new Date());
        invoiceSpecification.setUpdateTime(new Date());
        invoiceSpecification.setRechargeId(rechargeSheet.getRechargeId());
        invoiceSpecification.setEnterpriseId(invoicingDto.getEnterpriseId());
        System.out.println("invoiceSpecification--->"+invoiceSpecification);
        invoiceSpecificationDao.insertInvoiceSpecification(invoiceSpecification);
        //添加地址表
        TicketAddress ticketAddress = new TicketAddress();
        ticketAddress.setEnterpriseId(invoicingDto.getEnterpriseId());
        ticketAddress.setAddressee(invoicingDto.getAddressee());
        ticketAddress.setPhone(invoicingDto.getPhone());
        ticketAddress.setLandLinePhone(invoicingDto.getLandLinePhone());
        ticketAddress.setAddress(invoicingDto.getAddress());
        ticketAddress.setRechargeId(rechargeSheet.getRechargeId());
        System.out.println("ticketAddress--->"+ticketAddress);
        ticketAddressDao.insertticketAddress(ticketAddress);
        return 1;
    }

    @Override
    public PageInfo<InvoiceSpecification> invoiceList(Integer enterpriseId, String invoicereqTime, Integer invoiceStatus,Integer invoiceType , Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<InvoiceSpecification> invoiceList = invoiceSpecificationDao.invoiceList(enterpriseId,invoicereqTime,invoiceStatus,invoiceType);
        return new PageInfo<>(invoiceList);
    }

    @Override
    public List<InvoiceSpecification> findById(Integer enterpriseId) {
        return invoiceSpecificationDao.findById(enterpriseId);
    }

    @Override
    public List<InvoiceSpecification> easyExcel(Integer enterpriseId) {
        String fileName = "invoiceSpecification.xlsx";
        EasyExcel.write(fileName,InvoiceSpecification.class).sheet("发票申请表").doWrite(invoiceSpecificationDao.easyExcel(enterpriseId));
        return invoiceSpecificationDao.easyExcel(enterpriseId);
    }
}